import React, {Component} from 'react'
import {View, Text, AsyncStorage, Modal, TextInput, TouchableOpacity} from 'react-native'
import AppNavigation from './routes'
import NavigatorService from './navigate_service'
import {connect} from 'react-redux'
import Storage from 'react-native-storage'
import PersonalLogin from '../components/personal/login'
import {localLogin, fetchSettings} from '../redux/actions/user'
import Toast from 'react-native-root-toast'
import store from '../redux/store/store'

//在这里对组件的默认Props进行更改
Modal.defaultProps = {
    ...Modal.defaultProps,
    supportedOrientations: ['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']
}

TextInput.defaultProps = {
    ...TextInput.defaultProps,
    underlineColorAndroid: "transparent"
}

TouchableOpacity.defaultProps = {
    ...TouchableOpacity.defaultProps,
    hitSlop: {left: 5, right: 5, top: 5, bottom: 5}
}

//初始化数据库
var storage = new Storage({
    // 最大容量，默认值1000条数据循环存储
    size: 1000,

  
    // 存储引擎：对于RN使用AsyncStorage，对于web使用window.localStorage
    // 如果不指定则数据只会保存在内存中，重启后即丢失
    storageBackend: AsyncStorage,
      
    // 数据过期时间(秒)，设为null则永不过期
    defaultExpires: null,
      
    // 读写时在内存中缓存数据。默认启用
    enableCache: true,
        
})

//注意，global对象相当于浏览器里面的window，不要把过于庞大的函数对象放在这里面，应该放一些小而常用的
global.App = {
    awsEndPoint: 'https://s3.cn-north-1.amazonaws.com.cn',
    awsBucketName: 'cn.wfl-ischool.app.oralv1',
    //host: 'http://test.oral2.wfl-ischool.cn',
    //host: 'http://172.29.7.212:4567',
    version: '2.6.1.2',
    //host: 'http://172.29.11.250:4567'
    host: 'http://172.29.11.253:4567'

}
global.storage = storage
global.Toast = Toast


class AppInitialize extends Component {
    async componentWillMount () {
        const result = await this.props.dispatch(localLogin())
        //在这里获取用户的登录状态，根据登录状态来判断返回主界面还是登录界面
        if (result == true) {
            this.props.dispatch(fetchSettings())
            global.current_user = this.props.user.user
        }
    }

    render () {
        if (this.props.user.status != 'logged_in') {
            return (<PersonalLogin />
            )
        } else {
            return(
                <AppNavigation
                    ref={navigatorRef => {NavigatorService.setContainer(navigatorRef)}}
                />
            )
        }
    }
}

const mapStateToProps = (state) => {
    return {
        user: state.user
    }
}

export default connect(mapStateToProps)(AppInitialize)